from django.db import models
from django.db.models import permalink
from tagging.fields import TagField
from sorl.thumbnail.fields import ImageWithThumbnailsField
from managers import PublicManager
import datetime

class Page(models.Model):
    """
    A page of your portfolio.
    """
    
    title       = models.CharField(max_length=255)
    slug        = models.SlugField(unique=True)
    description = models.TextField(blank=True, help_text="What is this page all about anyway?")
    tags        = TagField()
    public      = models.BooleanField(default=True, help_text="Do you want this page to show up on the site right now?")
    created     = models.DateTimeField(auto_now_add=True)
    updated     = models.DateTimeField(auto_now=True)
    objects     = PublicManager()
    
    def __unicode__(self):
        return self.title

class Client(models.Model):
    """
    A Client is someone for whom you created a Project.
    """
    
    name        = models.CharField(max_length=255)
    slug        = models.SlugField(unique=True)
    url         = models.URLField('URL', blank=True)
    description = models.TextField(blank=True, help_text="Anything noteworthy about this client?")
    created     = models.DateTimeField(auto_now_add=True)
    updated     = models.DateTimeField(auto_now=True)
    
    class Meta:
        ordering = ('name',)
    
    def __unicode__(self):
        return self.name

class Category(models.Model):
    """
    A Category for a Project. (Print, Web, Music, etc.)
    """
    
    title       = models.CharField(max_length=255)
    slug        = models.SlugField(unique=True)
    description = models.TextField(blank=True)
    created     = models.DateTimeField(auto_now_add=True)
    updated     = models.DateTimeField(auto_now=True)
    
    class Meta:
       verbose_name_plural = 'categories'
    
    def __unicode__(self):
        return self.title

class Award(models.Model):
    """
    Awards, recognition for a project.
    """
    
    title       = models.CharField(max_length=255)
    slug        = models.SlugField(unique=True)
    description = models.TextField(blank=True)
    url         = models.URLField('URL', blank=True, verify_exists=True)
    created     = models.DateTimeField(auto_now_add=True)
    updated     = models.DateTimeField(auto_now=True)
    
    def __unicode__(self):
        return self.title

class Task(models.Model):
    """
    The role you played on the project (designer, coder, etc.)
    """
    
    title       = models.CharField(max_length=255)
    slug        = models.SlugField(unique=True)
    description = models.TextField(blank=True)
    order       = models.IntegerField(blank=True, null=True)
    created     = models.DateTimeField(auto_now_add=True)
    updated     = models.DateTimeField(auto_now=True)
    
    class Meta:
        ordering = ('title',)
    
    def __unicode__(self):
        return self.title

# http://code.djangoproject.com/changeset/7331
def get_default():
    return Page.objects.get(id=1)

class Project(models.Model):
    """
    A portfolio Project.
    """
    
    client      = models.ForeignKey(Client)
    page        = models.ForeignKey(Page, default=get_default)
    title       = models.CharField(max_length=255)
    slug        = models.SlugField(unique=True, help_text="example.com/portfolio/slug")
    description = models.TextField(blank=True, help_text="What's unique about this project?")
    url         = models.URLField('URL', blank=True)
    categories  = models.ManyToManyField(Category, blank=True)
    tasks       = models.ManyToManyField(Task, blank=True, help_text="For what tasks were you responsible on this project?")
    awards      = models.ManyToManyField(Award, blank=True, help_text="Props / Mentions / Recognition.")
    launch      = models.DateField('Launch Date', blank=True, null=True)
    tags        = TagField()
    thumbnail   = models.ImageField(upload_to='img/portfolio/thumbnails/', help_text="Only use this if you don't want to use automatically generated thumbnails from the images you upload below.", blank=True)
    public      = models.BooleanField(default=True, help_text="Do you want this project to show up on the site right now?")
    created     = models.DateTimeField(auto_now_add=True)
    updated     = models.DateTimeField(auto_now=True)
    order       = models.IntegerField(blank=True, null=True)
    objects     = PublicManager()
    
    def __unicode__(self):
        return self.title
    
    class Meta:
        ordering = ('order',)
    
    @models.permalink
    def get_absolute_url(self):
        return ('portfolio_detail', (), {'slug': self.slug})

def nice_images(instance, filename):
    """
    Let's give our image files nice names, shall we?
    
    http://scottbarnham.com/blog/2008/08/25/dynamic-upload-paths-in-django/
    """
    # Get the file extension:
    import os
    ext = os.path.splitext(filename)[1]
    if(instance.slug):
        return 'img/portfolio/%s_%s%s' % (instance.project.slug, instance.slug, ext)
    else:
        return 'img/portfolio/%s_%s' % (instance.project.slug, filename)

class Picture(models.Model):
    """
    An image from a Project
    """
    
    project = models.ForeignKey(Project)
    image   = ImageWithThumbnailsField(upload_to=nice_images, thumbnail={'size': (200, 200)})
    caption = models.CharField(blank=True, max_length=255)
    slug    = models.SlugField(blank=True, help_text="Use this to give your image file a nice name. --> img/portfolio/projectname_slug.jpg")
    tags    = TagField()
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)
    order   = models.IntegerField(blank=True, null=True)
    
    def __unicode__(self):
        return self.caption
    
    class Meta:
        ordering = ('order',)
